package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TLinkLabelConfig;
	import com.NeuglsWorkStudio.Utility.Utility;
	
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class TLinkLabel extends TLabel
	{
		private var _FURL:String;
		public static var MaxLinkDisplaySize:int=32;
		
		public function TLinkLabel(AConfig:TConfig)
		{
			super(AConfig);
		}

		public function get URL():String
		{
			return _FURL;
		}

		public function set URL(value:String):void
		{
			_FURL = value;
		}
		
		//************************************************************
		private function GetLinkConfig():TLinkLabelConfig{
			return _FConfig as TLinkLabelConfig;
		}
		
		protected override function Init():void{
			super.Init();
			_FURL=GetLinkConfig().URL;
			_FTextField.autoSize=TextFieldAutoSize.LEFT;
			if(GetLinkConfig().Normal==null){
				var textformat1:TextFormat=this.ATextFormat;
				textformat1.underline=false;
				ATextFormat=textformat1;
			}
			this.buttonMode=true;
			this.mouseChildren=false;
			this.addEventListener(MouseEvent.MOUSE_OUT,HandleMouseEvent,false,0,true);
			this.addEventListener(MouseEvent.CLICK,HandleMouseEvent,false,0,true);
			this.addEventListener(MouseEvent.MOUSE_OVER,HandleMouseEvent,false,0,true);
		}
		
		protected function HandleMouseEvent(E:MouseEvent):void{
			switch(E.type){
				case MouseEvent.CLICK:
					if(_FURL!=""){
						navigateToURL(new URLRequest(Utility.ValidateURL(_FURL)),GetLinkConfig().LinkTarget);						
					}
					break;
				case MouseEvent.MOUSE_OVER:
					if(GetLinkConfig().MouseOver==null){
						var textformat:TextFormat=this.ATextFormat;
						textformat.underline=true;
						ATextFormat=textformat;
					}else{
						ATextFormat=GetLinkConfig().MouseOver;
					}
					break;
				case MouseEvent.MOUSE_OUT:
					if(GetLinkConfig().Normal==null){
						var textformat1:TextFormat=this.ATextFormat;
						textformat1.underline=false;
						ATextFormat=textformat1;
					}else{
						ATextFormat=GetLinkConfig().Normal;
					}
					break;
			}
		}
		
		override protected  function Paint():void{
		}
		
		override protected function ProcessCaption(val:String):String{
			var result:String;
			if(val.length>MaxLinkDisplaySize){
				result=val.slice(0,15)+'...'+val.slice(val.length-12,val.length);
			}else{
				result=val;
			}
			return result;
		}
	}
}